from django.apps import AppConfig
from confluent_kafka import Consumer

from django.conf import settings
from apscheduler.schedulers.background import BackgroundScheduler
from VideoHandler.kafkaUtils.video_consumer import consumer_message

import logging

logger = logging.getLogger('VideoHandler')
import os

class VideoHandlerConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "VideoHandler"

    def ready(self):
        scheduler = BackgroundScheduler()
        if os.environ.get('RUN_MAIN'):
            consumer = Consumer(settings.KAFKA_CONF)

            consumer.subscribe(settings.KAFKA_TOPICS)

            scheduler = BackgroundScheduler()

            scheduler.add_job(consumer_message, 'interval', args=[consumer], seconds=1, id='consumer_integrate_video')

            scheduler.start()